﻿namespace WillSortSimulation;

public class SchoolGenerator
{
    public School[] GenerateSchoolBySort(int schoolNum, int totalWanted)
    {
        var result = new School[schoolNum];

        // 填入录取人数
        var leftNumber = totalWanted;
        
        var average = totalWanted / schoolNum;
        var min = average / 2;
        var max = (int)(average * 1.5);
        
        for (int i = 0; i < schoolNum; i++)
        {
            if (leftNumber < max)
            {
                max = leftNumber;
            }

            if (leftNumber < min)
            {
                min = leftNumber / 2;
            }

            int number = RandomManager.Get().Next(min, max);
            if (i == schoolNum - 1)
            {
                number = leftNumber;
            }
            result[i] = new School();
            result[i].StudentWanted = number;
            result[i].SchoolValue = RandomManager.Get().Next(60, 100);
            leftNumber -= number;
            Console.WriteLine($"{i}, \tnum: {number}, \tmin:{min}, \tmax:{max}, \tleft: {leftNumber}");
        }
        
        Array.Sort(result, School.CompareValue);

        for (int i = 0; i < schoolNum; i++)
        {
            result[i].SchoolID = i;
        }
        return result;
    }
}